package cn.com.connected.vo;

import cn.com.connected.common.emnus.FileType;
import cn.com.connected.common.emnus.StatusType;
import cn.com.connected.common.emnus.Whether;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.experimental.Tolerate;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Created by sms on 2019/6/19.
 */
@Data
@Builder
@ApiModel(description = "零散的字典 主要是枚举类型")
public class Dicts {

    @ApiModelProperty("字典版本")
    private String dictVersion;

    @ApiModelProperty("删除禁用状态")
    private List<Map> status;
    public static final String statusKeyName = "status";

    @ApiModelProperty("是否")
    private List<Map> whether;
    public static final String whetherKeyName = "whether";

    @ApiModelProperty("文件类型")
    private List<Map> fileType;
    public static final String fileTypeKeyName = "fileType";


    public static Dicts dictsVO = Dicts.builder()
            .status(StatusType.toMap())
            .fileType(FileType.toMap())
            .whether(Whether.toMap())
            .build();


    @SneakyThrows
    public static String dict(String dictType, Number dictValue){

        if (dictType==null || dictType ==null){
            return null;
        }

        List<Map> types = Collections.emptyList();
        Field[] fields = Dicts.class.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            if (dictType.equals(field.getName())){
                types = (List<Map> )field.get(dictsVO);
            }
        }

        String name = (String) types.stream().filter(o->o.get("value").equals(dictValue)).findFirst().map(z->z.get("name")).orElse(null);
        return name;
    }

    @Tolerate
    public Dicts() {
    }
}
